//
//  BMWCache.m
//  GTMC_CarOwner_Yonyou
//
//  Created by mac on 16/8/27.
//  Copyright © 2016年 wangbing. All rights reserved.
//

#import "ICreamCache.h"

@implementation ICreamCache

static ICreamCache *instance;
+(instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance=[[ICreamCache alloc]init];
    });
    return instance;
}
-(instancetype)init{
    self=[super init];
    if (self) {
        NSString *cachesDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        self.cachePath=[cachesDirectory stringByAppendingPathComponent:@"CMCache"];
        _cmQueue=dispatch_queue_create("CMCache.queue", NULL);
        //        NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:[self cachePathForKey:@"CMCache.file"]];
        //        if ([dict isKindOfClass:[NSDictionary class]]) {
        //            _cacheDictionary=[dict mutableCopy];
        //        }else{
        //            _cacheDictionary=[[NSMutableDictionary alloc]init];
        //        }
        if ([[NSFileManager defaultManager] fileExistsAtPath:self.cachePath]) {
            [[NSFileManager defaultManager] createDirectoryAtPath:self.cachePath withIntermediateDirectories:YES attributes:nil error:nil];
        }
        
    }
    return self;
}

- (NSString *)cachePathForKey:(NSString *)key
{
    NSString * pathName = self.cachePath;
    
    if ( NO == [[NSFileManager defaultManager] fileExistsAtPath:pathName] )
    {
        [[NSFileManager defaultManager] createDirectoryAtPath:pathName
                                  withIntermediateDirectories:YES
                                                   attributes:nil
                                                        error:NULL];
    }
    
    return [pathName stringByAppendingPathComponent:key];
}
- (void)removeDataForKey:(NSString *)key
{
    dispatch_async(_cmQueue, ^
                   {
                       [[NSFileManager defaultManager] removeItemAtPath:[self cachePathForKey:key] error:nil];
                   });
    
}
-(void)saveData:(NSData *)data forKey:(NSString *)key{
    if (!data ||!key) {
        return;
    }
    if ([[NSFileManager defaultManager] fileExistsAtPath:[self cachePathForKey:key]]) {
        [self removeDataForKey:key];
    }
    dispatch_async(_cmQueue, ^{
        [[NSFileManager defaultManager] createFileAtPath:[self cachePathForKey:key] contents:data attributes:nil];
    });
}

-(void)saveDataInGloable:(NSData *)data forKey:(NSString *)key{
    if (!data ||!key) {
        return;
    }
    if ([[NSFileManager defaultManager] fileExistsAtPath:[self cachePathForKey:key]]) {
        [self removeDataForKey:key];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSFileManager defaultManager] createFileAtPath:[self cachePathForKey:key] contents:data attributes:nil];
    });
    
}
-(NSData *)dataForKey:(NSString *)key{
    if ([[NSFileManager defaultManager] fileExistsAtPath:[self cachePathForKey:key]]) {
        return [NSData dataWithContentsOfFile:[self cachePathForKey:key] options:0 error:nil];
    }
    else return nil;
}

- (void)saveArrayInGloable:(NSArray *)array forKey:(NSString *)key
{
    NSData *data=[NSKeyedArchiver archivedDataWithRootObject:array];
    [self saveDataInGloable:data forKey:key];
}
- (void)saveArray:(NSArray *)array forKey:(NSString *)key
{
    NSData *data=[NSKeyedArchiver archivedDataWithRootObject:array];
    [self saveData:data forKey:key];
}
- (void)savedic:(NSDictionary *)dic forKey:(NSString *)key
{
    NSData *data=[NSKeyedArchiver archivedDataWithRootObject:dic];
    [self saveData:data forKey:key];
}
-(NSDictionary *)dictionaryForKey:(NSString *)key{
    NSData *data=[self dataForKey:key];
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
-(NSArray *)arrayForKey:(NSString *)key{
    NSData *data=[self dataForKey:key];
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
- (void)saveString:(NSString *)string forKey:(NSString *)key
{
    NSData *data=[string dataUsingEncoding:NSUTF8StringEncoding];
    [self saveData:data forKey:key];
}
- (NSString *)stringForKey:(NSString *)key
{
    NSData *data = [self dataForKey:key];
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

@end
